<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="选择栏目" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="选择栏目" clearable filterable  size="small" >
          <el-option value="" label="全部"></el-option>
          <el-option
            v-for="dict in categoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
     
      <el-form-item label="发布时间" prop="publishDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.publishDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发布时间">
        </el-date-picker>
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
     
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id"  align="center" />
      <el-table-column label="栏目名称" align="left" prop="categoryId" >
        <template slot-scope="scope">
          <span>{{ selectDictLabel(categoryOptions, scope.row.categoryId) }}</span>
        </template>
      </el-table-column>   
      <el-table-column label="文章标题" align="left" prop="title" width="350" />
      <el-table-column label="封面图" align="center" prop="imageHref" >
        <template slot-scope="scope">
          <el-image v-if="scope.row.imageHref"
            style="width: 60px; height: auto"
            :src="scope.row.imageHref" 
            :preview-src-list="[scope.row.imageHref]">
          </el-image>
        </template>
      </el-table-column>         
      
      <el-table-column label="标签" align="left" prop="label" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="序号" align="center" prop="serialNumber" />
      <el-table-column label="发布时间" align="center" prop="publishDate" width="140">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="views" />
      <el-table-column label="点赞量" align="center" prop="likes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="14">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="文章编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入文章编码" />
            </el-form-item>
          </el-col>
        </el-row>
       
        <el-row :gutter="20">
          <el-col :span="14">
            <el-form-item label="选择栏目" prop="categoryId">
              <el-select v-model="form.categoryId" placeholder="选择栏目" clearable filterable  size="small">
                <el-option
                  v-for="dict in categoryOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="标签" prop="label">
              <el-input v-model="form.label" placeholder="请输入标签" />
            </el-form-item>
             <el-form-item label="摘要" prop="summary">
                <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" />
              </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="封面图" prop="imageHref">
              <imageUpload v-model="form.imageHref"/>
              <div style="color: #ff0000;">封面图尺寸要求500*300px</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
             <el-form-item label="内容">
                <editor v-model="form.content" :min-height="192" :height="200"/>
              </el-form-item>
          </el-col>
         
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author" placeholder="请输入作者" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="序号" prop="serialNumber">
                <el-input v-model="form.serialNumber" placeholder="请输入序号" />
              </el-form-item>
          </el-col>
        </el-row>
     
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle, exportArticle } from "@/api/business/article";
import Editor from '@/components/Editor';
import ImageUpload from '@/components/ImageUpload';
export default {
  name: "Article",
  components: {
    Editor,
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章管理表格数据
      articleList: [],
      categoryOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        code: null,
        imageHref: null,
        summary: null,
        contentHtml: null,
        content: null,
        categoryId: null,
        publish: null,
        publishDate: null,
        author: null,
        label: null,
        serialNumber: null,
        isLink: null,
        link: null,
        isShow: null,
        isTop: null,
        views: null,
        likes: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
     this.getDicts("tb_artcle_category").then(response => {
      this.categoryOptions = response.data;
    });
  },
  methods: {
    /** 查询文章管理列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        code: null,
        imageHref: null,
        summary: null,
        contentHtml: null,
        content: null,
        categoryId: null,
        publish: null,
        publishDate: null,
        author: '',
        label: null,
        serialNumber: 0,
        isLink: null,
        link: null,
        isShow: null,
        isTop: null,
        views: null,
        likes: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除文章管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delArticle(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有文章管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportArticle(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
<style >
  img {
    width: 100%;
  }
</style>
